package org.csu.onlineedubackend.mapper;

import org.apache.ibatis.annotations.*;
import org.csu.onlineedubackend.entity.Notification;

import java.util.List;

@Mapper
public interface NotificationMapper {
    @Insert("INSERT INTO notifications (user_id, type, title, content, target_name, target_id, operator_name, group_id, group_name, is_owner, is_read, created_at) " +
            "VALUES (#{userId}, #{type}, #{title}, #{content}, #{targetName}, #{targetId}, #{operatorName}, #{groupId}, #{groupName}, #{isOwner}, #{isRead}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Notification notification);

    @Select("SELECT * FROM notifications WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Notification> findByUserId(Integer userId);

    @Update("UPDATE notifications SET is_read = true WHERE user_id = #{userId}")
    int markAllAsRead(Integer userId);

    @Update("UPDATE notifications SET is_read = true WHERE id = #{id} AND user_id = #{userId}")
    int markAsRead(@Param("id") Long id, @Param("userId") Integer userId);

    @Delete("DELETE FROM notifications WHERE user_id = #{userId} AND created_at < DATE_SUB(NOW(), INTERVAL 30 DAY)")
    int deleteOldNotifications(Integer userId);

    @Delete("DELETE FROM notifications WHERE user_id = #{userId} AND type = #{type} AND target_name = #{targetName}")
    int deleteByTypeAndTarget(@Param("userId") Integer userId, @Param("type") String type, @Param("targetName") String targetName);

    @Update("UPDATE notifications SET type = #{newType} WHERE id = #{id}")
    int updateType(@Param("id") Long id, @Param("newType") String newType);

    @Update("UPDATE notifications SET type = #{newType} WHERE user_id = #{userId} AND type = #{oldType} AND target_name = #{targetName}")
    int updateTypeByUserAndTarget(@Param("userId") Integer userId, @Param("oldType") String oldType, @Param("targetName") String targetName, @Param("newType") String newType);
} 